package com.mudu.search.result;

import cn.hutool.core.util.StrUtil;
import com.mudu.search.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

@ApiModel(value = "返回类")
@Data
@Builder
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -1850473388169342606L;

    @ApiModelProperty(value = "结果码")
    private Integer code;

    @ApiModelProperty(value = "结果信息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public Result() {
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <X> Result<X> ok() {
        return ok(ResultEnum.SUCCESS);
    }

    public static <X> Result<X> ok(X data) {
        return ok(ResultEnum.SUCCESS, data);
    }

    public static <X> Result<X> ok(Integer code, String message, X data) {
        return new Result<X>(code, message, data);
    }

    public static <X> Result<X> ok(Integer code, String message) {
        return new Result<X>(code, message);
    }

    public static <X> Result<X> ok(ResultEnum resultEnum) {
        return new Result<X>(resultEnum.getCode(), resultEnum.getMessage());
    }

    public static <X> Result<X> ok(ResultEnum resultEnum, X data) {
        return new Result<X>(resultEnum.getCode(), resultEnum.getMessage(), data);
    }


    public static <X> Result<X> error() {
        return error(ResultEnum.ERROR);
    }

    public static <X> Result<X> error(String message) {
        if (StrUtil.isBlank(message)) {
            return error(ResultEnum.ERROR);
        }
        return new Result<X>(ResultEnum.ERROR.getCode(), message);
    }

    public static <X> Result<X> error(Integer code, String message) {
        return new Result<X>(code, message);
    }

    public static <X> Result<X> error(ResultEnum resultEnum) {
        return new Result<X>(resultEnum.getCode(), resultEnum.getMessage());
    }

    public static <X> Result<X> error(ResultEnum resultEnum, X data) {
        return new Result<X>(resultEnum.getCode(), resultEnum.getMessage(), data);
    }
}
